<?php

class Survey{
	
	function _construct(){
		/*$this->id = $id;
		$query = mysql_query("SELECT * FROM  `survey` WHERE `id` = '$id'") or die (mysql_error);
		$survey = mysql_fetch_object($query);
		
		if($survey!=''){
			$this->name = $survey->survey_name;
			$this->title = $survey->survey_title;
			$this->welcome_message = $survey->welcome_message;
			$this->confirmation_message = $survey->confirmation_message;
		}*/
	}

	
	public function getSurvey($id){
		$this->id = $id;
		$query = mysql_query("SELECT * FROM  `survey` WHERE `id` = '$id'") or die (mysql_error);
		$s = mysql_fetch_object($query);
		
		$survey['name']= $s->survey_name;
		$survey['title']=$s->survey_title;
		$survey['welcome_message']=$s->welcome_message;
		$survey['confirmation_message']= $s->confirmation_message;
		$survey['email_subject']=$s->email_subject;
		$survey['email_message']=$s->email_message;
		
		if($survey){
			return $survey;
		}else{
			return false;
		}
	}
	
	public function createSurvey($data){
    $instructor = $_SESSION['logged_in'];	
	$test = "SELECT * FROM `survey` WHERE `survey_name` = '{$data['survey_name']}' AND `instructor_id` = '$instructor';";
    $check = mysql_query($test)or die ('TEST insert Survey'.mysql_error());
    $rows = mysql_num_rows($check);
      	
    if($rows ==0){ 
 	$sql = "INSERT INTO `survey` (`instructor_id`,`survey_name`,`survey_title`,`welcome_message`,`confirmation_message`,
	                               `email_subject`,`email_message`,`date_created`)
			VALUES (
            '{$data['instructor_id']}',
			'{$data['survey_name']}',
			'{$data['survey_title']}',
			'{$data['survey_welcome_message']}',
			'{$data['survey_confirmation_message']}',
			'{$data['email_subject']}',
			'{$data['email_message']}',
			'{$data['date_created']}'
			);";

	$result = mysql_query($sql) or die('Create Survey: '.mysql_error());

	return true;
    }else{
    	return false;
    }
	
}


public function updateSurvey($sid,$data){
    $instructor = $_SESSION['logged_in'];	
	//$this->id = $sid;
       
    $sql = "UPDATE `survey` SET `survey_name`='{$data['survey_name']}',`survey_title` = '{$data['survey_title']}',`welcome_message` = '{$data['survey_welcome_message']}',
 	                            `confirmation_message` = '{$data['survey_confirmation_message']}',`email_subject` = '{$data['email_subject']}',
 	                            `email_message`= '{$data['email_message']}',`last_modified`= '{$data['last_modified']}' 
 	                            WHERE `id`='$sid'";
			
	$result = mysql_query($sql) or die('Update Survey: '.mysql_error());
	if($result){
	return true;
    }else{
    	return false;
    }
	
}

public function getQuestions($id){
	$sql = "SELECT * FROM `question` WHERE `survey_id` = '$id' ORDER BY `order`";
	$result = mysql_query($sql) or die('Get All Questions '.mysql_error());
    while($r = mysql_fetch_object($result)) {
    $question[] = $r;
      }
      
      return $question;
}


public function getTotalNumberOfResponses($id){
	$sql = "SELECT * FROM `response` WHERE `survey_id` = '$id'";
	$result = mysql_query($sql) or die("getting number of responses".mysql_error());
    $total_response = mysql_num_rows($result);

    if($total_response){
    	return $total_response;
    }else{
    	return false;
    }
}

public function getResponses($id){
	$sql = "SELECT * FROM `response` WHERE `survey_id` = '$id' ";
    $result = mysql_query($sql)or die(mysql_error());
    
    while($r = mysql_fetch_object($result)) {
            $responses[] =$r;
        }

    if($responses){
    	return $responses;
    }else{
    	return false;
   }    
 }

   public function getAllConditions($id){
   $sql = "SELECT * FROM `branching` WHERE `survey_id` = '$id'";
   	$result = mysql_query($sql)or die("Check Condition:".mysql_error());
    $i=0;
   	while($c = mysql_fetch_object($result)){
   		$condition[$i]['id'] = $c->id;
   		$condition[$i]['question_1_id'] = $c->question_1_id;
   		$condition[$i]['question_2_id'] = $c->question_2_id;
   		$condition[$i]['condition']=$c->condition;
   		$condition[$i]['condition_value']= $c->value;
   		$i++;
   	}
   	if($condition){
   	return $condition;
   	}else{
   		return false;
   	}
   }

}

?>